#import "DDHelper.h"
#import "DDPlatform.h"

NSString *const HTMLGAME = @"H5";
@interface DDHelper()
{
    //    NSMutableDictionary *_localInfo;
    NSMutableArray *_accountArr;
    NSMutableArray *_a;
}
@end
@implementation DDHelper
static DDHelper*_helper;
static dispatch_once_t JTWHelperToken;
static DDHelperPara _para;
DDHelperManager helpermanger()
{
    DDHelperManager manager =
    {
        _help,
        _getPara,
        _setPara,
        _insertAccountDict
    };
    return manager;
}
DDHelper * _help()
{
    if (!_helper)
    {
        dispatch_once(&JTWHelperToken,^{
            _helper = [[DDHelper alloc]init];
            [CCNotice addObs:_helper sel:@selector(getIP:) name:CCNoticeNameIP obj:nil];
            [CCNotice addObs:_helper sel:@selector(getMoveTouch:) name:CCNoticeNameMoveTouchClicked obj:nil];
        });
    }
    return  _helper;
}
DDHelperPara _getPara()
{
    static dispatch_once_t para_token;
    dispatch_once(&para_token, ^{
        _para.localInfo = _localInfo();
        _para.accountArr = _accountArr();
        //        _para.account = _account();
        //        _para.accountID = _accountID();
        //        _para.token = _token();
        //        _para.gameAccountID = _gameAccountID();
        _para.appID = _appID();
        _para.channelID = _channelID();
        _para.serviceHeader = _serviceHeader();
        _para.key = _key();
        _para.country = _country();
        _para.region = _region();
        _para.city = _city();
        //        _para.name = _name();
        //        _para.phone = _phone();
        _para.color = _color();
        //        _para.state = _state();
        //        _para.extstate = _extstate();
        //        _para.hidestate = _hidestate();
        //        _para.auditstate = _auditstate();
        //        _para.bindstate = _bindstate();
        //        _para.link = _link();
        //        _para.qq = _qq();
                _para.ish5 = @"";
        _para.userBtn = _userBtn();
        
    });
    return _para;
}
static void _setPara(DDHelperPara para)
{
    _para = para;
}
static void _insertAccountDict(NSDictionary *dict)
{
    _para = helpermanger().getPara();
    _para.account = [dict valueForKey:@"name"];
    NSString *password = [dict valueForKey:@"password"];
    if (_para.account && password)
        [_para.accountArr yjd_enumerate:@{_para.account:password}];
}
NSDictionary *_localInfo()
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    NSString *wifi = [[CCIP IP].SSID hasPrefix:@"非wifi"]?[NSString stringWithFormat:@"%@/%@/%@", [CCDevice version], [CCDevice deviceType], [CCDevice networkType]]:[CCIP IP].SSID;
    [dict setValue:[CCDevice UUID] forKey:@"imei"];
    [dict setValue:[CCIP IP].wanIP?[CCIP IP].wanIP:@"" forKey:@"ip"];
    [dict setValue:wifi forKey:@"network"];
    [dict setValue:_para.appID forKey:@"appid"];
    [dict setValue:_para.channelID forKey:@"channelaccountid"];
    [dict setValue:@"1"forKey:@"terminal"];
    return dict;
}
NSMutableArray *_accountArr()
{
    return [[NSUserDefaults standardUserDefaults] mutableArrayValueForKey:@"JTWTokenKey"];
}
UIColor *_color()
{
    return [UIColor purpleColor];
}
NSString *_appID()
{
    return [NSString stringWithUTF8String:platformManager().getPara().appID];
}
NSString *_channelID()
{
    return [NSString stringWithUTF8String:platformManager().getPara().channelID];
}
NSString *_serviceHeader()
{
    return [NSString stringWithUTF8String:platformManager().getPara().serviceHeader];
}
NSString *_key()
{
    return [NSString stringWithUTF8String:platformManager().getPara().key];
}
NSString *_country()
{
    return [CCIP IP].wanCountry;
}
NSString *_city()
{
    return [CCIP IP].wanCity;
}
NSString *_region()
{
    return [CCIP IP].wanRegion;
}
CCMoveTouch *_userBtn()
{
    CCMoveTouch *button = [CCMoveTouch touch];
    button.hidden = YES;
    button.image = [@"centerLogo" yjd_image];
    return button;
}
- (void)getIP:(NSNotification *)notify
{
    _para.localInfo = _localInfo();
    _para.country = _country();
    _para.region = _region();
    _para.city = _city();
}
- (void)getMoveTouch:(NSNotification *)notify
{
    UIViewController *vc = (UIViewController *)[UIViewController currentVC];
    if (![NSStringFromClass([vc class]) isEqualToString:@"DDPersonViewController"])
    {
        UIViewController *presentingVC = [NSClassFromString(@"DDPersonViewController") new];
        presentingVC.modalTransitionStyle =  UIModalTransitionStyleCoverVertical;
        presentingVC.modalPresentationStyle = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)?UIModalPresentationFormSheet:UIModalPresentationOverCurrentContext;
        presentingVC.view.backgroundColor = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)?[UIColor whiteColor]:[UIColor clearColor];
        [vc presentViewController:presentingVC animated:YES completion:nil];
    }
}

@end
